#define _CRT_SECURE_NO_WARNINGS 1
#include<stdio.h>
#include<stdbool.h>


struct TreeNode {
    int val;
    struct TreeNode* left;
    struct TreeNode* right;
};
 
int TreeHeight(struct TreeNode* root)
{
    if (!root)
        return 0;
    int left = TreeHeight(root->left);
    int right = TreeHeight(root->right);
    return left > right ? left + 1 : right + 1;
}
bool isBalanced(struct TreeNode* root) {
    if (!root)
        return true;
    int sub = abs(TreeHeight(root->left) - TreeHeight(root->right));
    if (sub > 1)
        return false;
    return isBalanced(root->left) && isBalanced(root->right);
}